package com.hmdp.service.impl;

import cn.hutool.json.JSONUtil;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<ShopType> queryTypeList() {
        String typeListKey = RedisConstants.CACHE_TYPE_LIST_KEY;
        String json = stringRedisTemplate.opsForValue().get(typeListKey);
        if (json != null) {
            log.warn("命中缓存");
            return JSONUtil.toList(json , ShopType.class);
        }
        log.debug("未命中缓存");
        List<ShopType> typeList = query().orderByAsc("sort").list();
        if (typeList == null) {
            return null;
        }
        stringRedisTemplate.opsForValue().set(typeListKey, JSONUtil.toJsonStr(typeList));
        return typeList;
    }
}
